package com.example.sdbtspringboot.controller;

import com.example.sdbtspringboot.common.AuthAccess;
import com.example.sdbtspringboot.common.Page;
import com.example.sdbtspringboot.common.Result;
import com.example.sdbtspringboot.entity.User;
import com.example.sdbtspringboot.service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户表controllerv层
 * @Author: cao
 * @Date: 2024/5/20 10:27
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        try {
            userService.insertUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException)
                return Result.error("插入信息错误");
            else
                return Result.error("系统错误");
        }
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        try {
            userService.updateUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException)
                return Result.error("修改信息信息错误");
            else
                return Result.error("系统错误");
        }
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        try {
            userService.deleteUser(id);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException)
                return Result.error("修改信息信息错误");
            else
                return Result.error("系统错误");
        }
        return Result.success();
    }

    //批量删除
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        try {
            userService.batchDeleteUser(ids);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException)
                return Result.error("修改信息信息错误");
            else
                return Result.error("系统错误");
        }
        return Result.success();
    }

    //查询全部
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> userlist;
        try {
          userlist = userService.selectAll();
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException)
                return Result.error("修改信息信息错误");
            else
                return Result.error("系统错误");
        }
        return Result.success(userlist);
    }
    //按照id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@RequestParam Integer id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }
    //按照姓名查询
    //注意同名有多个对象，所以数据类型用list
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@RequestParam String name) {
        List<User> userlist = userService.selectByName(name);
        return Result.success(userlist);
    }
    //多条件查询
    @GetMapping("/selectByMore")
    public Result selectByMore(@RequestParam String name,@RequestParam String username) {
        List<User> userlist = userService.selectByMore(name,username);
        return Result.success(userlist);
    }
    //多条件模糊查询（模糊查询可以查到字段值不全的记录，如输入name的值“ca”，也可以查到name值为“cao”的多条记录）
    @GetMapping("/selectByMo")
    public Result selectByMo(@RequestParam String name,@RequestParam String username) {
        List<User> userlist = userService.selectByMo(name,username);
        return Result.success(userlist);
    }
    //多条件分页模糊查询
    //pageNum页码数
    //pagesize
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pagesize,
                               @RequestParam String name,
                               @RequestParam String username) {
        Page<User> page = userService.selectByPage(pageNum, pagesize, name, username);
        return Result.success(page);
    }
}
